package com.zhibang.sinocare.test.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zhibang.sinocare.common.domian.Detection;
import com.zhibang.sinocare.common.result.Result;
import com.zhibang.sinocare.common.result.ResultCode;
import com.zhibang.sinocare.test.service.DetectionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *接口
 */
@RestController
@RequestMapping("/detection")
@Api(tags = "用户管理模快",description = "对用户进行CRUD操作")
@CrossOrigin
public class DetectionController {
    @Resource
    private DetectionService detectionServiceImpl;

//    分页查询
    @RequestMapping(value = "find", method = RequestMethod.GET)
    @ApiOperation(value = "查询用户", notes = "根据条件分页查询", httpMethod = "GET")
    public Result findUsers(@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "limit",defaultValue = "5") Integer limit, String searchParams) throws Exception {
        System.out.println("进入了find方法");
        System.out.println(searchParams);
        //如果searchParams为null或空字符串，表示不带条件查询
        //通过Jackson将JSON字符串转换成实体对象
        ObjectMapper objectMapper = new ObjectMapper();
        //考虑分页：mybatis中的分页插件：PageHelper
        Detection detection = null;
        if (searchParams != null) {
            //将searchParams接收到的JSON字符串转换UserVo对象
            detection = objectMapper.readValue(searchParams, Detection.class);
        }

        Page page1 = PageHelper.startPage(page, limit);
        List<Detection> detections = detectionServiceImpl.selectDetection(detection);
        //将Page对象中的count值赋给Result对象
        return Result.ok(ResultCode.SUCCESS,page1.getTotal(),detections);
    }
//
//    @ApiOperation(value = "查询用户名中包含有\"a\"的男生",notes = "根据id查询用户详细信息",httpMethod = "GET")
//    @RequestMapping(value = "findBoy",method = RequestMethod.GET)
//    public Result findBoy(Detection detection) throws Exception{
//
//        //创建条件构造器
//        QueryWrapper<Detection> wrapper = new QueryWrapper<>();
//        //为条件构造器设置条件
//        wrapper.like("name",detection.getName());
//        wrapper.eq("phone",detection.getPhone());
//        //调用mybatis-plus提供方法
//        List<Detection> users = detectionServiceImpl.list(wrapper);
//        return Result.ok(ResultCode.SUCCESS,users);
//
//    }
//    //模糊查询
//    @ApiOperation(value = "查询姓名、电话",notes = "根据id查询用户详细信息",httpMethod = "GET")
//    @RequestMapping(value = "findBoy",method = RequestMethod.GET)
//    public Result findBoy(@RequestParam(value = "page",defaultValue = "1") Integer page,@RequestParam(value = "limit",defaultValue = "5") Integer limit,String searchParams )throws Exception{
//        //创建条件结构器
//        System.out.println("进入条件查询");
//        System.out.println("前端条件："+searchParams);
//        //为条件结构器设置条件
//        ObjectMapper objectMapper = new ObjectMapper();
//        //考虑分页：mybatis中的分页插件：PageHelper
//        Detection detection = null;
//        if (searchParams != null) {
//            //将searchParams接收到的JSON字符串转换UserVo对象
//            detection = objectMapper.readValue(searchParams, Detection.class);
//        }
//        Page page1 = PageHelper.startPage(page, limit);
//        //调用mybatis-plus提供方法
//        List<Detection> lists = detectionServiceImpl.selectDetection(detection);
//        PageInfo pageInfo = new PageInfo(page1);
//        return Result.ok(ResultCode.SUCCESS,pageInfo.getTotal(),lists);
//
//    }



//    //添加
    @RequestMapping(value = "add",method = RequestMethod.POST)
    @ApiOperation(value = "添加检测者",notes = "添加检测者",httpMethod = "POST")
    public Result addDetection(Detection detection)throws Exception{
        System.out.println(detection);

//        int i = detectionServiceImpl.save(detection);
        boolean save = detectionServiceImpl.save(detection);
        List<Detection> detections = detectionServiceImpl.selectDetection(detection);
        for (Detection detection1 : detections) {
            if (detection.getPatientId() == detection1.getPatientId()){
                detection.setPatientId(detection1.getPatientId()+10);
            }
        }



        if (save){
            return Result.ok(ResultCode.SUCCESS,save);
        }else {
            return Result.ok(ResultCode.FAIL,null);
        }
    }
//    //单个删除
    @RequestMapping(value = "{id}",method = RequestMethod.DELETE)
    @ApiOperation(value = "删除单个检测者",notes = "删除单个检测者",httpMethod = "DELETE")
    public Result deleteById(@PathVariable("id")Integer id) throws Exception{
        boolean i = detectionServiceImpl.removeById(id);
        if (i){
            return Result.ok(ResultCode.SUCCESS,i);
        }else {
            return Result.ok(ResultCode.FAIL,null);
        }
    }

    //修改
    @RequestMapping(value = "up",method = RequestMethod.POST)
    @ApiOperation(value = "修改检测者",notes = "修改检测者",httpMethod = "POST")
    public Result updateById(Detection detection) throws Exception{
        boolean b = detectionServiceImpl.updateById(detection);
        if (b){
            return Result.ok(ResultCode.SUCCESS,b);
        }else {
            return Result.ok(ResultCode.FAIL,null);
        }
    }



}
